
var evidencefiles = new Array();

function loadSkills(){
	var scIdNode = document.getElementById('skillcatid');
	var scid = scIdNode.options[scIdNode.selectedIndex].value;
	data = "scid=" + encodeURIComponent(scid);

	if (scid != 0){
		XMLRequestPostQ("skills/services.php?method=getskillsforcategory", data, loadSkillsCallback);
	} else {
		var sBox = document.getElementById("skillid");
		sBox.innerHTML = "";
	}
}


function loadSkillsCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}

	var sBox = document.getElementById("skillid");
	sBox.innerHTML = "";
	
	var skillsNode = xmlDoc.documentElement.getElementsByTagName('skills');
	var skills = skillsNode[0].getElementsByTagName('node');
	
	for (var i=0; i<skills.length; i++){
		var oValNode = skills[i].getElementsByTagName('ident');
		var oVal = oValNode[0].firstChild.nodeValue;
		
		var oNameNode = skills[i].getElementsByTagName('skillname');
		var oName = oNameNode[0].firstChild.nodeValue;
		
		var sOpt = document.createElement("option");
		sOpt.setAttribute("value", oVal);
		sOpt.appendChild(document.createTextNode(oName));
		sBox.appendChild(sOpt);
	}
}

function addEvidence(){
	var fileidNode = document.getElementById('fileid');
	var fileid = fileidNode.options[fileidNode.selectedIndex].value;
	//add to evidencefiles array
	var nextPos = evidencefiles.length;
	for(var i=0; i<nextPos ; i++){
		if(evidencefiles[i] == fileid){
			return;
		}
	}
	evidencefiles[nextPos] = fileid;
	//get the file details from db
	var data = "fileid="+encodeURIComponent(fileid);
	XMLRequestPostQ("skills/services.php?method=getfile", data, addEvidenceCallback);
}

function addEvidenceCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	var folder = getNodeValue(xmlDoc.documentElement,"folder");
	var fileId = getNodeValue(xmlDoc.documentElement,"ident");
	var originalName = getNodeValue(xmlDoc.documentElement,"originalname");
	var usId = getNodeValue(xmlDoc.documentElement,"usid");
	
	var titleNode = xmlDoc.documentElement.getElementsByTagName('title');
	if(titleNode[0].firstChild){
		var fileTitle = titleNode[0].firstChild.nodeValue;
	} else {
		var fileTitle = "";
	}
	if(usId == 0){
		var eList = document.getElementById("evidencelist");
	} else {
		var eList = document.getElementById("evidencelist"+usId);
	}
	var liFile = document.createElement("li");
	liFile.setAttribute("id",fileId);
	var aFile = document.createElement("a");
	aFile.setAttribute("target","_blank");
	
	if (fileTitle != ""){
		var filename=fileTitle;
	} else {
		var filename=originalName;
	}

	var targetFile = wwwroot + profileName + "/files/" + folder + "/" + fileId + "/"+originalName;
	aFile.setAttribute("href",targetFile);
	aFile.appendChild(document.createTextNode(filename));
	liFile.appendChild(aFile);
	
	if(usId != 0){
		var spanEvid = document.createElement("span");
		spanEvid.setAttribute("id",usId+"-"+fileId);
		changeNodeClass(spanEvid,"remove");
		if(spanEvid.addEventListener){ // Mozilla, Netscape, Firefox
			spanEvid.addEventListener('click', removeEvidenceEdit, false);
		} else { // IE
			spanEvid.attachEvent('onclick', removeEvidenceEdit);
		}
		spanEvid.appendChild(document.createTextNode("[remove]"));	
		liFile.appendChild(document.createTextNode(" "));	
		liFile.appendChild(spanEvid);
	}
		
	eList.appendChild(liFile);
}

function addSkill(){
	var data = "";
	// skill id
	var skillidNode = document.getElementById('skillid');
	if(skillidNode.selectedIndex == -1){
		alert("You must select a skill.");
		return;
	}
	var skillid = skillidNode.options[skillidNode.selectedIndex].value;
	data += "skillid=" + encodeURIComponent(skillid);
	
	// want to develop
	var skillwant = document.getElementById('skillwant');
	if (skillwant.checked == true){
		skillwant = "Y";
	} else {
		skillwant = "N";
	}
	data += "&skillwant=" +  encodeURIComponent(skillwant);
	
	//skill date
	if (skillwant == "N"){
		var skillDateD = document.getElementById('skilldate_day').value;
		data += "&dateday=" + encodeURIComponent(skillDateD);
		
		var skillDateM = document.getElementById('skilldate_month').value;
		data += "&datemonth=" + encodeURIComponent(skillDateM);
		
		var skillDateY = document.getElementById('skilldate_year').value;
		data += "&dateyear=" + encodeURIComponent(skillDateY);
		
		var skillDate = skillDateM.trim() + "/" + skillDateD.trim() + "/" + skillDateY.trim();
		if (!isDate(skillDate)){
			alert("Please enter a valid date");
			return;
		}
		
		//skill location
		var skilllocNode = document.getElementById('skilllocation');
		var skillLoc = skilllocNode.value.trim();
		if(skillLoc == ""){
			alert("You must enter a location.");
			return;
		}
		data += "&skilllocation=" + encodeURIComponent(skillLoc);
	}
		
	
	//skill notes
	var skillnotesNode = document.getElementById('skillnotes');
	var skillNotes = skillnotesNode.value.trim();
	data += "&skillnotes=" + encodeURIComponent(skillNotes);
	
	//evidence
	var skillEvid = evidencefiles.join(",");
	data += "&evidence=" + encodeURIComponent(skillEvid);
	XMLRequestPostQ("skills/services.php?method=addskill", data, addSkillCallback);
}

function addSkillCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	//clear text boxes
	var skillcatId = document.getElementById("skillcatid");
	skillcatId.selectedIndex = 0;
	var skillid = document.getElementById("skillid");
	skillid.innerHTML ="";
	document.getElementById("skilldate_day").value="dd";
	document.getElementById("skilldate_month").value="mm";
	document.getElementById("skilldate_year").value="yyyy";
	var skilllocation = document.getElementById("skilllocation");
	skilllocation.value ="";
	var skillnotes = document.getElementById("skillnotes");
	skillnotes.value ="";
	var skillevid = document.getElementById("evidencelist");
	skillevid.innerHTML ="";
	
	// clear evidenceArray
	evidencefiles = new Array();

	//add new row
	var newRow = createSkillRow(xmlDoc);
	var rowH =document.getElementById("skillrowheader");
	rowH.parentNode.appendChild(newRow);

}

function removeSkill(skillId,skillName){
	if (confirm("Are you sure you want to remove '"+skillName+"'?")){
		XMLRequestQ("skills/services.php?method=removeskill&skillid=" + skillId, removeSkillCallback);
	}
}


function removeSkillCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	// extract the Id
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var skillId = idNode[0].firstChild.nodeValue;
	
	//remove the node
	var row = document.getElementById('skillrow'+skillId);
	row.parentNode.removeChild(row);
}

function editSkill(usId){

	
	var usRow = document.getElementById("skillrow"+usId);
	var editTR = document.createElement("tr");
	editTR.setAttribute("id","editskillrow"+usId);
	changeNodeClass(editTR,"vp");
	
	var editTD = document.createElement("td");
	changeNodeClass(editTD,"vp");
	editTD.setAttribute("colspan","5");
	editTR.appendChild(editTD);
	
	var editDiv = document.createElement("div");
	editDiv.setAttribute("id","editskilldiv"+usId);
	changeNodeClass(editDiv,"editskilldiv");
	editDiv.appendChild(document.createTextNode("Loading edit form..."));
	editTD.appendChild(editDiv);

	usRow.parentNode.replaceChild(editTR,usRow);
	
	
	//load the skill
	XMLRequestQ("skills/services.php?method=getuserskill&usid=" + usId, editSkillCallback);
	
}

function editSkillCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	evidencefiles = new Array();
	var usId = getNodeValue(xmlDoc.documentElement,"ident");
	var skillId = getNodeValue(xmlDoc.documentElement,"skill_id");
	var skillName = getNodeValue(xmlDoc.documentElement,"skillname");
	var skillWant = getNodeValue(xmlDoc.documentElement,"us_want");
	var skillDate = getNodeValue(xmlDoc.documentElement,"us_date");
	var skillDateDay = getNodeValue(xmlDoc.documentElement,"us_date_day")
	var skillDateMonth = getNodeValue(xmlDoc.documentElement,"us_date_month")
	var skillDateYear = getNodeValue(xmlDoc.documentElement,"us_date_year")
	var skillLoc = getNodeValue(xmlDoc.documentElement,"us_location");
	var skillNotes = getNodeValue(xmlDoc.documentElement,"us_notes");
	var editDiv = document.getElementById("editskilldiv"+usId);
	editDiv.innerHTML = "";
	
	var h2 = createNode("h2","Edit Skill","");
	editDiv.appendChild(h2);
	
	// category
	var catDiv = document.createElement("div");
	changeNodeClass(catDiv,"infoholder");

	var fieldDiv = document.createElement("div");
	changeNodeClass(fieldDiv,"fieldname");
	var h3 = createNode("h3","Category","");
	fieldDiv.appendChild(h3);
	catDiv.appendChild(fieldDiv);
	
	var catP = document.createElement("p");
	var catSel = document.createElement("select");
	catSel.setAttribute("id","skillcatid"+usId);
	catSel.setAttribute("name","skillcatid"+usId);

	if(catSel.addEventListener){ // Mozilla, Netscape, Firefox
		catSel.addEventListener('change', function(){loadSkillsEdit(usId);}, false);
	} else { // IE
		catSel.attachEvent('onchange', function(){loadSkillsEdit(usId);});
	}

	var catOpt = document.createElement("option");
	catOpt.setAttribute("selected","true");
	catOpt.setAttribute("value","0");
	catOpt.appendChild(document.createTextNode(" "));
	catSel.appendChild(catOpt);
	catP.appendChild(catSel);
	catDiv.appendChild(catP);
	editDiv.appendChild(catDiv);
	
	// skill
	var skillDiv = document.createElement("div");
	changeNodeClass(skillDiv,"infoholder");

	var fieldDiv = document.createElement("div");
	changeNodeClass(fieldDiv,"fieldname");
	var h3 = createNode("h3","Skill","");
	fieldDiv.appendChild(h3);
	skillDiv.appendChild(fieldDiv);
	
	var skillP = document.createElement("p");
	var skillSel = document.createElement("select");
	skillSel.setAttribute("id","skillid"+usId);
	skillSel.setAttribute("name","skillid"+usId);

	var skillOpt = document.createElement("option");
	skillOpt.setAttribute("selected","true");
	skillOpt.setAttribute("value",skillId);
	skillOpt.appendChild(document.createTextNode(skillName));
	
	skillSel.appendChild(skillOpt);
	skillP.appendChild(skillSel);
	skillDiv.appendChild(skillP);
	editDiv.appendChild(skillDiv);
	
	// want
	var wantDiv = document.createElement("div");
	changeNodeClass(wantDiv,"infoholder");
	var wantP = document.createElement("p");
	var wantIn = document.createElement("input");
	wantIn.setAttribute("id","skillwant"+usId);
	wantIn.setAttribute("name","skillwant"+usId);
	wantIn.setAttribute("type","checkbox");
	wantIn.setAttribute("value","Y");
	if (skillWant == "Y"){
		wantIn.setAttribute("checked","checked");
	}
	wantP.appendChild(wantIn);
	wantP.appendChild(document.createTextNode("This is a skill I would like to develop"));
	wantDiv.appendChild(wantP);
	editDiv.appendChild(wantDiv);
	
	//date
	var dateDiv = document.createElement("div");
	changeNodeClass(dateDiv,"infoholder");

	var fieldDiv = document.createElement("div");
	changeNodeClass(fieldDiv,"fieldname");
	var h3 = createNode("h3","Date","");
	fieldDiv.appendChild(h3);
	dateDiv.appendChild(fieldDiv);
	
	var dateP = document.createElement("p");
	
	var dateInDay = document.createElement("input");
	dateInDay.setAttribute("id","skilldate_day"+usId);
	dateInDay.setAttribute("name","skilldate_day"+usId);
	dateInDay.setAttribute("type","text");
	dateInDay.setAttribute("size","1");
	if (skillDate != 0){
		dateInDay.setAttribute("value",skillDateDay);
	} else {
		dateInDay.setAttribute("value","");
	}
	dateP.appendChild(dateInDay);
	
	dateP.appendChild(document.createTextNode(" - "));
	
	var dateInMonth = document.createElement("input");
	dateInMonth.setAttribute("id","skilldate_month"+usId);
	dateInMonth.setAttribute("name","skilldate_month"+usId);
	dateInMonth.setAttribute("type","text");
	dateInMonth.setAttribute("size","1");
	if (skillDate != 0){
		dateInMonth.setAttribute("value",skillDateMonth);
	} else {
		dateInMonth.setAttribute("value","");
	}
	dateP.appendChild(dateInMonth);
	
	dateP.appendChild(document.createTextNode(" - "));
	
	var dateInYear = document.createElement("input");
	dateInYear.setAttribute("id","skilldate_year"+usId);
	dateInYear.setAttribute("name","skilldate_year"+usId);
	dateInYear.setAttribute("type","text");
	dateInYear.setAttribute("size","2");
	if (skillDate != 0){
		dateInYear.setAttribute("value",skillDateYear);
	} else {
		dateInYear.setAttribute("value","");
	}
	dateP.appendChild(dateInYear)
	
	dateDiv.appendChild(dateP);
	editDiv.appendChild(dateDiv);
	
	//location
	var locDiv = document.createElement("div");
	changeNodeClass(locDiv,"infoholder");

	var fieldDiv = document.createElement("div");
	changeNodeClass(fieldDiv,"fieldname");
	var h3 = createNode("h3","Location","");
	fieldDiv.appendChild(h3);
	locDiv.appendChild(fieldDiv);
	
	var locP = document.createElement("p");
	var locIn = document.createElement("input");
	locIn.setAttribute("id","skilllocation"+usId);
	locIn.setAttribute("name","skilllocation"+usId);
	locIn.setAttribute("type","text");
	locIn.setAttribute("value",skillLoc);
	changeNodeClass(locIn,"inputWidth");
	locP.appendChild(locIn);
	locDiv.appendChild(locP);
	editDiv.appendChild(locDiv);
	
	//notes
	var notesDiv = document.createElement("div");
	changeNodeClass(notesDiv,"infoholder");

	var fieldDiv = document.createElement("div");
	changeNodeClass(fieldDiv,"fieldname");
	var h3 = createNode("h3","Notes","");
	fieldDiv.appendChild(h3);
	notesDiv.appendChild(fieldDiv);
	
	var notesP = document.createElement("p");
	var notesIn = document.createElement("textarea");
	notesIn.setAttribute("id","skillnotes"+usId);
	notesIn.setAttribute("name","skillnotes"+usId);
	notesIn.appendChild(document.createTextNode(skillNotes));
	changeNodeClass(notesIn,"textinput");
	notesP.appendChild(notesIn);
	notesDiv.appendChild(notesP);
	editDiv.appendChild(notesDiv);
	
	//evidence
	var evidDiv = document.createElement("div");
	changeNodeClass(catDiv,"infoholder");

	var fieldDiv = document.createElement("div");
	changeNodeClass(fieldDiv,"fieldname");
	var h3 = createNode("h3","Evidence","");
	fieldDiv.appendChild(h3);
	evidDiv.appendChild(fieldDiv);
	var evidP = document.createElement("p");
	evidP.appendChild(createEvidenceList(xmlDoc, true, usId));
	
	var evidSel = document.createElement("select");
	evidSel.setAttribute("id","evidenceselect"+usId);
	evidSel.setAttribute("name","evidenceselect"+usId);

	evidP.appendChild(evidSel);
	
	evidAdd = document.createElement("input");
	evidAdd.setAttribute("type","button");
	evidAdd.setAttribute("value","Add");
	
	if(evidAdd.addEventListener){ // Mozilla, Netscape, Firefox
		evidAdd.addEventListener('click', function(){addEvidenceEdit(usId);}, false);
	} else { // IE
		evidAdd.attachEvent('onclick', function(){addEvidenceEdit(usId);});
	}
	
	evidP.appendChild(evidAdd);
	evidDiv.appendChild(evidP);
	editDiv.appendChild(evidDiv);
	
	//save/cancel buttons
	var buttonP = document.createElement("p");
	
	var btnSave = document.createElement("input");
	btnSave.setAttribute("type","button");
	btnSave.setAttribute("value","Save");
	if(btnSave.addEventListener){ // Mozilla, Netscape, Firefox
		btnSave.addEventListener('click', function(){saveSkillsEdit(usId);}, false);
	} else { // IE
		btnSave.attachEvent('onclick', function(){saveSkillsEdit(usId);});
	}
	
	var btnCancel = document.createElement("input");
	btnCancel.setAttribute("type","button");
	btnCancel.setAttribute("value","Cancel");
	if(btnCancel.addEventListener){ // Mozilla, Netscape, Firefox
		btnCancel.addEventListener('click', function(){cancelSkillsEdit(usId);}, false);
	} else { // IE
		btnCancel.attachEvent('onclick', function(){cancelSkillsEdit(usId);});
	}
	
	buttonP.appendChild(btnSave);
	buttonP.appendChild(btnCancel);
	editDiv.appendChild(buttonP);
	//load categories
	loadCategories(usId);
	//load files
	loadFiles(usId);
}

function cancelSkillsEdit(usid){
	var data = "usid=" + encodeURIComponent(usid);
	XMLRequestPostQ("skills/services.php?method=getuserskill", data, cancelSkillsEditCallback);
}

function cancelSkillsEditCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	// get the edit row
	var usId = getNodeValue(xmlDoc.documentElement,"ident");
	var editRow = document.getElementById("editskillrow"+usId);
	var usRow = createSkillRow(xmlDoc);
	editRow.parentNode.replaceChild(usRow,editRow);
	
}

function createSkillRow(xmlDoc){
	var usId = getNodeValue(xmlDoc.documentElement,"ident");
	var skillName = getNodeValue(xmlDoc.documentElement,"skillname");
	var skillDate = getNodeValue(xmlDoc.documentElement,"us_date");
	var skillLocation = getNodeValue(xmlDoc.documentElement,"us_location");
	var skillNotes = getNodeValue(xmlDoc.documentElement,"us_notes");
	var skillWant = getNodeValue(xmlDoc.documentElement,"us_want");
	
	var sRow = document.createElement("tr");
	sRow.setAttribute("id","skillrow"+usId);
	changeNodeClass(sRow,"vp");

	sRow.appendChild(createNode("td",skillName,"vp"));
	if (skillWant == "N"){
		sRow.appendChild(createNode("td",skillDate + ", " + skillLocation,"vp"));
	} else {
		sRow.appendChild(createNode("td","To develop","vp"));
	}
	sRow.appendChild(createNode("td",skillNotes,"vp"));

	var tdEvid = document.createElement("td");
	changeNodeClass(tdEvid,"vp");
	//add the evidence
	tdEvid.appendChild(createEvidenceList(xmlDoc,false,0));
	sRow.appendChild(tdEvid);
	
	// options
	var optT = document.createElement("td");
	changeNodeClass(optT,"vp");
	var removeSpan = document.createElement("span");
	if(removeSpan.addEventListener){ // Mozilla, Netscape, Firefox
		removeSpan.addEventListener('click', function(){removeSkill(usId,skillName);}, false);
	} else { // IE
		removeSpan.attachEvent('onclick', function(){removeSkill(usId,skillName);});
	}

	changeNodeClass(removeSpan,'remove');
	removeSpan.appendChild(document.createTextNode("[remove]"));
	optT.appendChild(removeSpan);

	var editSpan = document.createElement("span");
	if(editSpan.addEventListener){ // Mozilla, Netscape, Firefox
		editSpan.addEventListener('click', function(){editSkill(usId);}, false);
	} else { // IE
		editSpan.attachEvent('onclick', function(){editSkill(usId);});
	}
	changeNodeClass(editSpan,'remove');
	editSpan.appendChild(document.createTextNode("[edit]"));
	optT.appendChild(editSpan);
	sRow.appendChild(optT);
	
	return sRow;
}


function createEvidenceList(xmlDoc, withRemove, usId){
	var ulEvid = document.createElement("ul");
	changeNodeClass(ulEvid,"evidencelist");
	if (usId != 0){
		ulEvid.setAttribute("id","evidencelist"+usId);
	}
	var evidNode = xmlDoc.documentElement.getElementsByTagName('evidence');
	var evidFiles = evidNode[0].getElementsByTagName('node');

	for (var i=0; i<evidFiles.length; i++){
		var fIdent = getNodeValue(evidFiles[i],'ident');
		var fTitle = getNodeValue(evidFiles[i],'title');
		var fFolder = getNodeValue(evidFiles[i],'folder');
		var fOrigName = getNodeValue(evidFiles[i],'originalname');
		
		if(fTitle != ""){
			var fName = fTitle;
		} else {
			var fName = fOrigName;
		}
		var targetFile = wwwroot + profileName + "/files/" + fFolder + "/" + fIdent + "/"+fOrigName;
		var evidLi = document.createElement("li");
		evidLi.setAttribute("id",fIdent);
		var aEvid = document.createElement("a");
		aEvid.setAttribute("target","_blank");		
		aEvid.setAttribute("href",targetFile);
		aEvid.appendChild(document.createTextNode(fName));		
		evidLi.appendChild(aEvid);

		if(withRemove){
			var spanEvid = document.createElement("span");
			spanEvid.setAttribute("id",usId+"-"+fIdent);
			changeNodeClass(spanEvid,"remove");
			if(spanEvid.addEventListener){ // Mozilla, Netscape, Firefox
				spanEvid.addEventListener('click', removeEvidenceEdit, false);
			} else { // IE
				spanEvid.attachEvent('onclick', removeEvidenceEdit);
			}
			spanEvid.appendChild(document.createTextNode("[remove]"));		
			evidLi.appendChild(document.createTextNode(" "));
			evidLi.appendChild(spanEvid);
		}
		ulEvid.appendChild(evidLi);	
	}
	return ulEvid;
}


function removeEvidenceEdit(e){
	var e=e? e : window.event;
	var el=e.target? e.target : e.srcElement;
	var mySplit = el.id.split("-");
	var usId = mySplit[0];
	var fIdent = mySplit[1];
	var ulNode = document.getElementById("evidencelist"+usId);
	for (var i=0; i<ulNode.childNodes.length; i++){
		if (ulNode.childNodes[i].getAttribute("id") == fIdent){
			var removeNode = ulNode.childNodes[i];
			removeNode.parentNode.removeChild(removeNode);
			return;
		}
	}
}


function loadSkillsEdit(usId){
	var data = "usid=" + encodeURIComponent(usId);
	var scIdNode = document.getElementById('skillcatid'+usId);
	var scid = scIdNode.options[scIdNode.selectedIndex].value;
	data += "&scid=" + encodeURIComponent(scid);
	XMLRequestPostQ("skills/services.php?method=getskillsforcategory", data, loadSkillsEditCallback);
}

function loadSkillsEditCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	var usId = getNodeValue(xmlDoc.documentElement,"usid");
	var skillSel = document.getElementById("skillid"+usId);
	skillSel.innerHTML = "";
	var skillsNode = xmlDoc.documentElement.getElementsByTagName('skills');
	var skills = skillsNode[0].getElementsByTagName('node');
	//loop over skills and add to dropdown
	for (var i=0; i<skills.length; i++){
		var sIdent = getNodeValue(skills[i],'ident');
		var sName = getNodeValue(skills[i],'skillname');
		
		var sOpt = document.createElement("option");
		sOpt.setAttribute("value",sIdent);
		sOpt.appendChild(document.createTextNode(sName));
		
		skillSel.appendChild(sOpt);
	}
}

function loadCategories(usId){
	var data = "usid=" + encodeURIComponent(usId);
	XMLRequestPostQ("skills/services.php?method=getcategories", data, loadCategoriesCallback);
}

function loadCategoriesCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	var usId = getNodeValue(xmlDoc.documentElement,"usid");
	//get the category select dropdown
	var catSel = document.getElementById("skillcatid"+usId);
	var catsNode = xmlDoc.documentElement.getElementsByTagName('categories');
	var cats = catsNode[0].getElementsByTagName('node');
	
	//loop over categories and add to dropdown
	for (var i=0; i<cats.length; i++){
		var catIdent = getNodeValue(cats[i],'ident');
		var catName = getNodeValue(cats[i],'scname');
		
		var catOpt = document.createElement("option");
		catOpt.setAttribute("value",catIdent);
		catOpt.appendChild(document.createTextNode(catName));
		
		catSel.appendChild(catOpt);
	}
}

function loadFiles(usId){
	var data = "usid=" + encodeURIComponent(usId);
	XMLRequestPostQ("skills/services.php?method=getfiles", data, loadFilesCallback);
}

function loadFilesCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	var usId = getNodeValue(xmlDoc.documentElement,"usid");
	var fileSel = document.getElementById("evidenceselect"+usId);

	var filesNode = xmlDoc.documentElement.getElementsByTagName('files');
	var files = filesNode[0].getElementsByTagName('node');
	//loop over files and add to dropdown
	for (var i=0; i<files.length; i++){
		var fIdent = getNodeValue(files[i],'ident');
		var fTitle = getNodeValue(files[i],'title');
		var fOrigName = getNodeValue(files[i],'originalname');
		if(fTitle ==""){
			var fName = fOrigName;
		} else {
			var fName = fTitle;
		}
		
		var fOpt = document.createElement("option");
		fOpt.setAttribute("value",fIdent);
		fOpt.appendChild(document.createTextNode(fName));
		
		fileSel.appendChild(fOpt);
	}
}

function addEvidenceEdit(usId){
	//get file id
	var data = "usid=" + encodeURIComponent(usId);
	var fIdNode = document.getElementById('evidenceselect'+usId);
	var fIdent = fIdNode.options[fIdNode.selectedIndex].value;
	//check it's not already added
	var ulNode = document.getElementById("evidencelist"+usId);
	for (var i=0; i<ulNode.childNodes.length; i++){
		if (ulNode.childNodes[i].getAttribute("id") == fIdent){
			alert("This file has already been added.");
			return;
		}
	}
	data += "&fileid=" + encodeURIComponent(fIdent);
	XMLRequestPostQ("skills/services.php?method=getfile", data, addEvidenceCallback);
	
}

function saveSkillsEdit(usId){
	var data = "usid="+encodeURIComponent(usId);
	// skill id
	var skillidNode = document.getElementById('skillid'+usId);
	if(skillidNode.selectedIndex == -1){
		alert("You must select a skill.");
		return;
	}
	var skillid = skillidNode.options[skillidNode.selectedIndex].value;
	data += "&skillid=" + encodeURIComponent(skillid);
	
	// want to develop
	var skillwant = document.getElementById('skillwant'+usId);
	if (skillwant.checked == true){
		skillwant = "Y";
	} else {
		skillwant = "N";
	}
	data += "&skillwant=" +  encodeURIComponent(skillwant);
	
	//skill date
	if (skillwant == "N"){
		var skillDateD = document.getElementById('skilldate_day'+usId).value;
		data += "&dateday=" + encodeURIComponent(skillDateD);
		
		var skillDateM = document.getElementById('skilldate_month'+usId).value;
		data += "&datemonth=" + encodeURIComponent(skillDateM);
		
		var skillDateY = document.getElementById('skilldate_year'+usId).value;
		data += "&dateyear=" + encodeURIComponent(skillDateY);
		
		var skillDate = skillDateM.trim() + "/" + skillDateD.trim() + "/" + skillDateY.trim();
		if (!isDate(skillDate)){
			alert("Please enter a valid date");
			return;
		}
	
		//skill location
		var skilllocNode = document.getElementById('skilllocation'+usId);
		var skillLoc = skilllocNode.value.trim();
		if(skillLoc == ""){
			alert("You must enter a location.");
			return;
		}
		data += "&skilllocation=" + encodeURIComponent(skillLoc);
	}
	//skill notes
	var skillnotesNode = document.getElementById('skillnotes'+usId);
	var skillNotes = skillnotesNode.value.trim();
	data += "&skillnotes=" + encodeURIComponent(skillNotes);

	//evidence
	var tempSkillEvidFiles = new Array();
	//get the evidence list
	var ulNode = document.getElementById("evidencelist"+usId);
	for (var i=0; i<ulNode.childNodes.length; i++){
		
		tempSkillEvidFiles[i] = ulNode.childNodes[i].getAttribute("id");
	}
	var tempSkillEvid = tempSkillEvidFiles.join(",");
	data += "&evidence=" + encodeURIComponent(tempSkillEvid);

	XMLRequestPostQ("skills/services.php?method=editskill", data, saveSkillsEditCallback);
}

function saveSkillsEditCallback(xmlDoc){
	if (!xmlDoc){
		return;
	}
	var usId = getNodeValue(xmlDoc.documentElement,"ident");
	var editRow = document.getElementById("editskillrow"+usId);
	var usRow = createSkillRow(xmlDoc);
	editRow.parentNode.replaceChild(usRow,editRow);
}